<template>
  <div class="tab-container">

    <el-form :inline="true"
             :model="listQuery"
             class="demo-form-inline">
      <el-form-item label="活动名称">
        <el-input v-model="listQuery.keyword"
                  placeholder="输入活动名称"
                  style="width: 200px;"
                  class="filter-item" />
      </el-form-item>

      <el-form-item label="交易日期">
        <el-date-picker v-model="dateRange"
                        type="daterange"
                        align="right"
                        unlink-panels
                        range-separator="至"
                        start-placeholder="开始日期"
                        end-placeholder="结束日期"
                        value-format="yyyy-MM-dd"
                        :picker-options="pickerOptions">
        </el-date-picker>
      </el-form-item>

      <el-form-item>
        <el-button type="primary"
                   icon="el-icon-search"
                   @click="filterHandle">查询</el-button>
      </el-form-item>
    </el-form>

    <el-tag>mounted times ：{{ createdTimes }}</el-tag>
    <el-alert :closable="false"
              style="width:200px;display:inline-block;vertical-align: middle;margin-left:30px;"
              title="Tab with keep-alive"
              type="success" />
    <el-tabs v-model="activeName"
             style="margin-top:15px;"
             type="border-card">
      <el-tab-pane v-for="item in tabMapOptions"
                   :key="item.key"
                   :label="item.label"
                   :name="item.key">
        <keep-alive>
          <tab-pane v-if="activeName==item.key"
                    :type="item.key"
                    @create="showCreatedTimes" />
        </keep-alive>
      </el-tab-pane>
    </el-tabs>
  </div>
</template>

<script>
import tabPane from './components/TabPane'

export default {
  name: 'Tab',
  components: {tabPane},
  data() {
    return {
      pickerOptions: {
        shortcuts: [
          {
            text: '最近一周',
            onClick(picker) {
              const end = new Date()
              const start = new Date()
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 7)
              picker.$emit('pick', [start, end])
            },
          },
          {
            text: '最近一个月',
            onClick(picker) {
              const end = new Date()
              const start = new Date()
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 30)
              picker.$emit('pick', [start, end])
            },
          },
          {
            text: '最近三个月',
            onClick(picker) {
              const end = new Date()
              const start = new Date()
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 90)
              picker.$emit('pick', [start, end])
            },
          },
        ],
      },
      dateRange: [],

      tabMapOptions: [
        {label: 'China', key: 'CN'},
        {label: 'USA', key: 'US'},
        {label: 'Japan', key: 'JP'},
        {label: 'Eurozone', key: 'EU'},
      ],
      activeName: 'CN',
      createdTimes: 0,

      list: null,
      total: 0,
      listLoading: true,
      listQuery: {
        page: 1,
        page_size: 10,
      },
    }
  },
  watch: {
    activeName(val) {
      this.$router.push(`${this.$route.path}?tab=${val}`)
    },
  },
  created() {
    // init the default selected tab
    const tab = this.$route.query.tab
    if (tab) {
      this.activeName = tab
    }
  },
  methods: {
    showCreatedTimes() {
      this.createdTimes = this.createdTimes + 1
    },
  },
}
</script>

<style scoped>
.tab-container {
  margin: 30px;
}
</style>
